package com.lbb.springboot.auto.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;


public class AutoGenerationSpringMVC2 {

    static String tableName = "biu_recharg".trim();
    static String author = "李康鸿";
    static String className = tableName.substring(tableName.indexOf("_") + 1, tableName.length());
    static String classNameLowerCase = DomysqlBean.replaceLine(className.toLowerCase());
//    static String project = "xuliyouka";
    static String project = "xcx";
    static String page = "com.lbb." + project;
    static String name = DomysqlBean.toUpperCaseFirstOne(DomysqlBean.replaceLine(className));
    private static final Logger logger = LoggerFactory.getLogger(AutoGenerationSpringMVC2.class);


    public static void main(String[] args) throws SQLException {

        List<FormData.FieldData> lf = DomysqlBean.autoFieldData(tableName);

        // 生成实体类
        DomysqlBean.autoBean(tableName);

        String iDaoBean = IDaoBean(name);
        String iServiceBean = IServiceBean(name);
        String iServiceImplBean = IServiceImplBean(name);
        String controller = controller(name);

//        String mapper = AutoGenerationMapper.getMapper(lf,tableName);

//        logger.info("controller:"+controller);
        // 生成controller
//		saveFile(name + "Controller", controller, "controller");
//        // 生成dao
        saveFile("" + name + "Mapper", iDaoBean, "Mapper");
//         生成service
        saveFile("I" + name + "Service", iServiceBean, "service");
        saveFile(name + "ServiceImpl", iServiceImplBean, "service/impl");


    }


    public static String getDeatilsText() {
        String text = "";
        text += "\t@ApiOperation(value = \"详情\") \r";
        text += "\t@PostMapping(value = \"/get\") \r";
        text += "\tpublic ServiceResult<" + name + "> get(@RequestParam(value = \"id\")  String id) { \r";
        text += "\t" + name + "	 " + classNameLowerCase + "= " + classNameLowerCase + "Service.getById(id); \r";
        text += "\t	return  new ServiceResult<>(" + classNameLowerCase + "); \r";
        text += "\t} \r";
        return text;
    }

    public static String getSaveText() {
        String text = "";
        text += "\t@ApiOperation(value = \"保存\")\r";
        text += "\t@PostMapping(value = \"/save\")\r";
        text += "\tpublic ServiceResult save(@RequestBody " + name + " " + classNameLowerCase + ") {\r";
        text += "\t	boolean save = " + classNameLowerCase + "Service.saveOrUpdate(" + classNameLowerCase + ");\r";
        text += "\t	if (save) {\r";
        text += "\t		return ServiceResult.succ();\r";
        text += "\t	} else {\r";
        text += "\t		ServiceResult.fail();\r";
        text += "\t	}\r";
        text += "\t	return null;\r";
        text += "\t}\r";
        return text;

    }

    public static String getListPageText() {
        String text = "";
        text += "\t@ApiOperation(value = \"分页列表\")\r";
        text += "\t@PostMapping(\"listPage\")\r";
        text += "\tpublic ServiceResult<IPage<" + name + ">> listPage(@RequestBody BasePage basePage) {\r";
        text += "\t	QueryWrapper<" + name + "> queryWrapper = new QueryWrapper<>();\r";
        text += "\t	queryWrapper.eq(\"status\", 1);\r";
        text += "\t	queryWrapper.orderByDesc(\"create_date\");\r";

        text += "\t\tPage<" + name + "> newPage = new Page<>();\r";
        text += "\t\tnewPage.setCurrent(basePage.getCurrent());\r";
        text += "\t\tnewPage.setSize(basePage.getSize());\r";

        text += "\t	IPage<" + name + "> list = " + classNameLowerCase + "Service.page(newPage, queryWrapper);\r";
        text += "\t	return new ServiceResult<>(list);\r";
        text += "\t}\r";
        return text;
    }

    public static String getListText() {
        String text = "";
        text += "\t@ApiOperation(value = \"列表\")\r";
        text += "\t@PostMapping(\"list\")\r";
        text += "\tpublic ServiceResult<List<" + name + ">> list() {\r";
        text += "\t	QueryWrapper<" + name + "> queryWrapper = new QueryWrapper<>();\r";
        text += "\t	queryWrapper.eq(\"status\", 1);\r";
        text += "\t	queryWrapper.orderByDesc(\"create_date\");\r";
        text += "\t	List<" + name + "> list = " + classNameLowerCase + "Service.list(queryWrapper);\r";
        text += "\t	return new ServiceResult<>(list);\r";
        text += "\t}\r";
        return text;
    }


    public static String controller(String className) {
        String text = "";
        text += "package " + page + ".controller;                                 \r";
        text += "import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;    \r";
        text += "import com.baomidou.mybatisplus.core.metadata.IPage;    \r";
        text += "import com.baomidou.mybatisplus.extension.plugins.pagination.Page;    \r";
        text += "import com.lbb.springboot.entity.ServiceResult;    \r";
        text += "import com.lbb.springboot.entity.vo.BasePage;   \r";
        text += "import com.lbb."+project+".entity.dto." + className + ";\r";
        text += "import com.lbb."+project+".service.I" + className + "Service;    \r";
        text += "import io.swagger.annotations.Api;    \r";
        text += "import org.slf4j.Logger; \r";
        text += "import org.slf4j.LoggerFactory; \r";
        text += "import io.swagger.annotations.ApiOperation;    \r";
        text += "import org.springframework.beans.factory.annotation.Autowired;    \r";
        text += "import org.springframework.web.bind.annotation.*;    \r";
        text += "import java.util.List;    \r";
        text += "/**                                                                 \r";
        text += " *                                                                  \r";
        text += " *                                                                  \r";
        text += " * @author " + author + "                                                 \r";
        text += " * @version 0.0.1                                                   \r";
        text += " * @since                                                           \r";
        text += " */                                                                 \r";
//		text += "@Controller                                                                                                                  \r";
//		text += "@RequestMapping(\"/" + classNameLowerCase + "\")                   \r";
        text += " @Api(description = \"" + classNameLowerCase + "接口\")   \r";
        text += " @RestController   \r";
        text += " @RequestMapping(\"/" + classNameLowerCase + "\")   \r";
        text += " public class " + className + "Controller {                              \r";
        text += "                                                                    \r";
        text += "   private static final Logger logger = LoggerFactory.getLogger(" + className + "Controller.class);\r";
        text += "	@Autowired                                                         \r";
        text += "	private I" + className + "Service " + classNameLowerCase + "Service;  \r";
        text += "                                                                    \r";
        text += "                                                                    \r";

        text += "                                                                    \r";

        text += getDeatilsText();
        text += "                                                                    \r";

        text += getSaveText();
        text += "                                                                    \r";

        text += getListPageText();
        text += "                                                                    \r";
        text += getListText();
        text += "                                                                    \r";
        text += "}                                                                   \r";
        text += "                                                                    \r";

        return text;
    }


    public static String IServiceBean(String className) {
        String text = "";
        text += "package " + page + ".service;                                \r";
        text += "import com.baomidou.mybatisplus.extension.service.IService;\n\r";
        text += "import " + page + ".entity.dto." + className + ";                    \r";
        text += "/**                                                         \r";
        text += " *                                                          \r";
        text += " *                                                          \r";
        text += " * @author  " + author + "                                        \r";
        text += " * @version 1.0.0                                           \r";
        text += " * @since                                                   \r";
        text += " * @Date " + DateUtil.getCurrtime() + "                                                 \r";
        text += " */                                                         \r";
        text += "public interface I" + className + "Service   extends IService<" + className + ">  {                   \r";
        text += "	                                                           \r";

        text += "}                                                            \r";
        return text;
    }

    public static String IServiceImplBean(String className) {
        String text = "";
        text += "package " + page + ".service.impl;                                \r";
        text += "import org.springframework.stereotype.Service;\r";
        text += "import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;\r";
        text += "import com.lbb."+project+".mapper." + className + "Mapper;\r";
        text += "import com.lbb."+project+".service.I" + className + "Service;\r";
        text += "import org.slf4j.Logger; \r";
        text += "import org.slf4j.LoggerFactory; \r";
        text += "import " + page + ".entity.dto." + className + ";                    \r";
        text += "/**                                                         \r";
        text += " * @author  " + author + "                                        \r";
        text += " * @version 1.0.0                                           \r";
        text += " * @since                                                   \r";
        text += " * @Date " + DateUtil.getCurrtime() + "                                                 \r";
        text += " */                                                         \r";
        text += " @Service                                                \r";
        text += "public class " + className + "ServiceImpl   extends ServiceImpl<" + className + "Mapper," + className + ">    implements I" + className + "Service  {                   \r";
        text += "	                                                           \r";
        text += "    private static final Logger logger = LoggerFactory.getLogger(" + className + "ServiceImpl.class);\r";
        text += "}                                                            \r";
        return text;
    }


    public static String IDaoBean(String className) {
        String text = "";
        text += "package " + page + ".mapper;                                \r";
        text += "import com.lbb.springboot.SuperMapper;\n\r";
        text += "import " + page + ".entity.dto." + className + ";                    \r";
        text += "/**                                                         \r";
        text += " *                                                          \r";
        text += " *                                                          \r";
        text += " * @author  " + author + "                                      \r";
        text += " * @version 1.0.0                                           \r";
        text += " * @since                                                   \r";
        text += " * @Date " + DateUtil.getCurrtime() + "                                                 \r";
        text += " */                                                         \r";
        text += "public interface " + className + "Mapper   extends SuperMapper<" + className + ">  {                   \r";
        text += "	                                                           \r";
        text += "}                                                            \r";
        return text;
    }


    public static void saveFile(String classname, String text, String pageName) {

        System.out.println("text:" + text);
        System.out.println("创建java类：" + classname);
        String filepath = Thread.currentThread().getContextClassLoader().getResource(".").getPath();

        System.out.println("filepath:" + filepath);

        String[] fileP = filepath.split("target");
        System.out.println(fileP[0]);

        File file = new File(fileP[0] + "/src/main/java/com/lbb/"+project+"/" + pageName + "/"
                + FormData.firstUpper(classname) + ".java");


        if (file.exists()) {
            file.delete();
        }

        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdir();
        }

        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedWriter writer;
        try {
            writer = new BufferedWriter(new FileWriter(new File(file.getPath())));
            writer.write(text);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
